package com.findtech.threePomelos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.avos.avoscloud.AVUser;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.database.OperateDBUtils;
import com.findtech.threePomelos.entity.BabyInfoEntity;
import com.findtech.threePomelos.entity.TravelInfoEntity;
import com.findtech.threePomelos.login.LoginActivity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.utils.RequestUtils;

/**
 * Created by zhi.zhang on 3/16/16.
 */
public class LaunchActivity extends Activity implements OperateDBUtils.SaveBabyInfoFinishListener {

    private final String TAG = "LaunchActivity";
    private OperateDBUtils operateDBUtils;
    private NetWorkRequest netWorkRequest;
    private boolean isShowOnce = false; //是否已经显示一次了
    private final int TIMEOUT = 0x01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        MyApplication.getInstance().addActivity(this);
        operateDBUtils = new OperateDBUtils(this);
        operateDBUtils.setSaveBabyInfoFinishListener(this);
        netWorkRequest = new NetWorkRequest(this);
        if (AVUser.getCurrentUser() != null && netWorkRequest.isNetworkConnected()) {
            netWorkRequest.getTravelInfoDataAndSaveToDB();
            netWorkRequest.getTotalMileageDataAndSaveToSP();
            netWorkRequest.getBabyHeightDataAndSaveToDB();
            netWorkRequest.getBabyWeightDataAndSaveToDB();
            netWorkRequest.getBabyInfoDataAndSaveToDB();
        }
        Message message = new Message();
        message.what = TIMEOUT;
        mHandle.sendMessageDelayed(message, 2000);
    }

    Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case TIMEOUT:
                    if (AVUser.getCurrentUser() != null) {
                        Log.d("ZZ", "BabyInfoEntity.getInstance().getIsBind() = "
                                + BabyInfoEntity.getInstance().getIsBind());
                        operateDBUtils.queryBabyInfoDataFromDB();
                        operateDBUtils.queryUserHeightData();
                        operateDBUtils.queryUserWeightData();
                        operateDBUtils.queryTravelInfoDataFromDB();
                        TravelInfoEntity travelInfoEntity = TravelInfoEntity.getInstance();
                        travelInfoEntity.setTotalMileage(RequestUtils.getSharepreference(LaunchActivity.this)
                                .getString(OperateDBUtils.TOTAL_MILEAGE, "0.0"));
                    } else {
                        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                    }
                    break;
            }
        }
    };

    @Override
    public void saveBabyInfoFinish() {
        Log.d("ZZ", "saveBabyInfoFinish getIsBind = "
                + BabyInfoEntity.getInstance().getIsBind());
        if (!isShowOnce) {
            if (BabyInfoEntity.getInstance().getIsBind()) {
                startActivity(new Intent(LaunchActivity.this, HomeActivity.class));
            } else {
                Intent intent = new Intent(LaunchActivity.this, BindCarActivity.class);
                intent.putExtra("from", "LaunchActivity");
                startActivity(intent);
            }
            isShowOnce = true;
        }
    }
}
